﻿using adb;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using PowerStation.RequestModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Z.EntityFramework.Plus;

namespace PowerStation.TableModel
{
    /// <summary>
    /// 统计类型
    /// </summary>
    public enum DataCacheEnum { 
        累加型 = 0x001,
        平均型 = 0x010,
        单独计算 = 0x100
    }
    /// <summary>
    /// 数据缓存类型
    /// </summary>
    public class DataCache
    {
        /// <summary>
        /// 构造
        /// </summary>
        private adb.dbBase db;
        private MyDbContext cacheDb;
        public DataCache() {
            db = new dbBase();
            cacheDb = this.db.CreateMyDb();
        }
        public DataCacheEnum type { get; set; } = DataCacheEnum.累加型;
        private Dictionary<int, decimal> powerDayValue = new Dictionary<int, decimal>();
        /// <summary>
        /// 月发电数据,kw.h
        /// </summary>
        private Dictionary<int,decimal> powerMonthValue = new Dictionary<int, decimal>();
        /// <summary>
        /// 年发电数据,kw.h
        /// </summary>
        private Dictionary<int, decimal> powerYearValue = new Dictionary<int, decimal>();

        /// <summary>
        /// 从数据库中获取数据
        /// </summary>
        private Func<DateTime,DateTime, long, long,string, DbContextOptionsBuilder<MyDbContext>, DbContextOptionsBuilder<DeviceDbContext>, Task<decimal>> GetValueFromDb;
        
        private decimal? getValueAsync(int code, Dictionary<int, decimal> dic) {
            //if (dic.ContainsKey(code)) return dic[code];
            //var m =  await cacheDb.tCache.FirstOrDefaultAsync(x => x.cacheName == code);
            //if (m != null)
            //{
            //    dic[code] = m.cacheValue;
            //}
            //return m?.cacheValue;
            return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private void setValueAsync(int name,decimal value,Dictionary<int,decimal> dic) {
            try
            {
                //dic[name] = value;
                //await cacheDb.tCache.AddAsync(new tCache { cacheName = name, cacheValue = value });
                //await cacheDb.SaveChangesAsync();
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 获取按小时来计算的值
        /// </summary>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="powerStationId">电站ID</param>
        /// <param name="areaId">区域ID</param>
        /// <param name="deviceSN">设备SN</param>
        /// <param name="reflushKey">刷新值</param>
        /// <returns></returns>
        public async Task<decimal> hourValue(DateTime start, DateTime end, long powerStationId = 0, long areaId = 0, string deviceSN = "", string reflushKey = "") {
            var v = await this.GetValueFromDb(start, end, powerStationId, areaId, deviceSN, db.myOptionsBuilder, db.deviceoOptionsBuilder);
            return v;
        }
        /// <summary>
        /// 天数据
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="powerStationId">电站ID,不传找所有电站</param>
        /// <param name="areaId">区域ID,不传就找所有区域</param>
        /// <param name="deviceSN"></param>
        /// <param name="reflushKey">数据刷新机制,如果跟上次传的不一样就重新从数据中获取</param>
        /// <returns></returns>
        public async Task<decimal> DayValue(DateTime date, long powerStationId = 0, long areaId = 0, string deviceSN = "", string reflushKey = "")
        {
            if (DateTime.Today <= date)
            {

                return await this.GetValueFromDb(date.Date,date.AddDays(1).Date, powerStationId, areaId, deviceSN, db.myOptionsBuilder, db.deviceoOptionsBuilder);
            }   
            TemDayData m1 = new TemDayData() { 
                areaId = areaId,
                deviceSN = deviceSN,
                powerStationId = powerStationId,
                reflushKey = reflushKey,
                time   = date,
            };
            var m = this.getValueAsync(m1.GetHashCode(),powerDayValue);
            if (m == null)
            {
                m1.value = await this.GetValueFromDb(date.Date, date.AddDays(1).Date, powerStationId, areaId, deviceSN, db.myOptionsBuilder, db.deviceoOptionsBuilder);
                this.setValueAsync(m1.GetHashCode(), m1.value,powerDayValue);
            }
            else {
                m1.value = m.Value;
            }

            return m1.value;
        }

        /// <summary>
        /// 月数据
        /// </summary>
        /// <param name="date"></param>
        /// <param name="powerStationId"></param>
        /// <param name="areaId"></param>
        /// <param name="deviceSN"></param>
        /// <param name="reflushKey">数据刷新机制,如果跟上次传的不一样就重新从数据中获取</param>
        /// <returns></returns>
        public async Task<decimal> MonthValue(DateTime date, long powerStationId = 0, long areaId = 0, string deviceSN = "", string reflushKey = "")
        {
            if (DateTime.Today.IsSameMonth(date))
            {
                if (this.type == DataCacheEnum.单独计算) { 
                    return await this.DayValue(date, powerStationId,areaId,deviceSN, reflushKey);
                }
                List<decimal> v = new List<decimal>();
                TemMonthData oldModel = new TemMonthData
                {
                    areaId = areaId,
                    deviceSN = deviceSN,
                    powerStationId = powerStationId,
                    reflushKey = $"{DateTime.Today:yyyyMMdd}",
                    time = date
                };
                if (powerMonthValue.ContainsKey(oldModel.GetHashCode()))
                {
                    oldModel.value = (powerMonthValue[oldModel.GetHashCode()]);
                    
                }
                else
                {
                    List<decimal> v2 = new List<decimal>();
                    for (DateTime i = new DateTime(date.Year, date.Month, 1); i.IsSameDay(DateTime.Today) == false; i = i.AddDays(1))
                    {
                        v2.Add(await this.DayValue(i, powerStationId, areaId,deviceSN,reflushKey));
                    }
                    if ((this.type & DataCacheEnum.累加型) == DataCacheEnum.累加型)
                    {
                        oldModel.value = (v2.Sum());
                    }
                    else if((this.type & DataCacheEnum.平均型) == DataCacheEnum.平均型) {
                        if (v2.Count() == 0)
                        {
                            oldModel.value = 0;
                        }
                        else
                        {
                            v2 = v2.Where(x => x > 0).ToList();//这里可能有BUG
                            oldModel.value = v2.Any()?(v2.Average()):0;
                        }
                    }
                    powerMonthValue[oldModel.GetHashCode()] = oldModel.value;
                }

                v.Add(oldModel.value);
                v.Add((await this.DayValue(DateTime.Today, powerStationId, areaId, deviceSN, reflushKey)));

                if ((this.type & DataCacheEnum.累加型) == DataCacheEnum.累加型)
                {
                        return v.Sum();
                }
                else if ((this.type & DataCacheEnum.平均型) == DataCacheEnum.平均型) {
                    if (!v.Any()) return 0;
                        return v.Average();
                }
                
                return v.Sum();
            }


            TemMonthData m1 = new TemMonthData { 
                areaId = areaId,
                deviceSN = deviceSN,
                powerStationId = powerStationId,
                reflushKey = reflushKey,
                time = date
            };
            var m = this.getValueAsync(m1.GetHashCode(),powerMonthValue);
            if (m == null)
            {

                List<decimal> v = new List<decimal>();
                if (this.type == DataCacheEnum.单独计算)
                {

                    for (DateTime i = new DateTime(date.Year, date.Month, 1); i.IsSameMonth(date); i = i.AddDays(1))
                    {
                        v.Add(await this.DayValue(i, powerStationId, areaId, deviceSN, reflushKey));
                    }
                }
                else {
                      v.Add( await this.DayValue(date, powerStationId, areaId, deviceSN, reflushKey));
                }
                if((this.type & DataCacheEnum.累加型) == DataCacheEnum.累加型)
                {
                     m1.value = v.Sum();
                }
                else if((this.type & DataCacheEnum.平均型) == DataCacheEnum.平均型)
                {
                    var cc = DateTime.DaysInMonth(date.Year, date.Month);
                    if (cc == 0)
                    {
                        m1.value = 0;
                    }
                    else {
                        m1.value = v.Sum() / cc;
                    }
                     
                }
                this.setValueAsync(m1.GetHashCode(), m1.value,powerMonthValue);
            }
            else {
                m1.value = m.Value;
            }
            return m1.value;
        }

        /// <summary>
        /// 年数据
        /// </summary>
        /// <param name="date"></param>
        /// <param name="powerStationId"></param>
        /// <param name="areaId"></param>
        /// <param name="deviceSN"></param>
        /// <param name="reflushKey">数据刷新机制,如果跟上次传的不一样就重新从数据中获取</param>
        /// <returns></returns>
        public async Task<decimal> YearValue(DateTime date, long powerStationId = 0, long areaId = 0, string deviceSN = "", string reflushKey = "")
        {
            if (DateTime.Today.IsSameYear(date))
            {
                if (this.type == DataCacheEnum.单独计算) { 
                    return await this.DayValue((DateTime)date, powerStationId, areaId, deviceSN, reflushKey);
                }

                List<decimal> v = new List<decimal>();
                for (DateTime i = new DateTime(date.Year, 1, 1); i.IsSameYear(date); i = i.AddMonths(1))
                {
                    v.Add(await this.MonthValue(i, powerStationId, areaId,deviceSN,reflushKey));
                }
                if ((this.type & DataCacheEnum.累加型) == DataCacheEnum.累加型)
                {
                    return v.Sum();
                }
                else if ((this.type & DataCacheEnum.平均型) == DataCacheEnum.平均型) {
                    v = v.Where(x => x > 0).ToList();
                    if (v.Any())
                    {
                        return v.Average();
                    }
                    else
                    {
                        return 0;
                    }
                }
            }


            TemYearData m1 = new TemYearData { 
                areaId = areaId,
                deviceSN = deviceSN,
                powerStationId = powerStationId,
                reflushKey = reflushKey,
                time = date
            };
            var  m = this.getValueAsync(m1.GetHashCode(),powerYearValue);
            if (m == null)
            {

                List<decimal> v = new List<decimal>();
                if (this.type == DataCacheEnum.单独计算)
                {
                    v.Add( await this.DayValue((DateTime)date, powerStationId, areaId, deviceSN, reflushKey));
                }
                else 
                {
                    for (DateTime i = new DateTime(date.Year, 1, 1); i.IsSameYear(date); i = i.AddMonths(1))
                    {
                        v.Add(await this.MonthValue(i, powerStationId, areaId, deviceSN, reflushKey));
                    }
                }
                if ((this.type & DataCacheEnum.累加型) == DataCacheEnum.累加型)
                {
                    m1.value = v.Sum();
                }
                else if ((this.type & DataCacheEnum.平均型) == DataCacheEnum.平均型)
                {
                    v = v.Where(x => x > 0).ToList();
                    if (v.Any())
                        m1.value = v.Average();
                    else
                        m1.value = 0;
                }
                this.setValueAsync(m1.GetHashCode(), m1.value,powerYearValue);
            
            }
            else {
                m1.value = m.Value;
            }
            return m1.value;
        }

        /// <summary>
        /// 天同比,返回100%
        /// </summary>
        /// <param name="date"></param>
        /// <param name="powerStationId"></param>
        /// <param name="areaId"></param>
        /// <param name="deviceSN"></param>
        /// <param name="reflushKey">数据刷新机制,如果跟上次传的不一样就重新从数据中获取</param>
        /// <returns></returns>
        public async Task<decimal> TongBiOfDay(DateTime date, long powerStationId = 0, long areaId = 0, string deviceSN = "", string reflushKey = "") { 
            var nowValue = await this.DayValue(date, powerStationId, areaId, deviceSN, reflushKey);
            var beforeValue = await this.DayValue(date.AddYears(-1), powerStationId, areaId, deviceSN, reflushKey);
            if (beforeValue == 0) return 0;
            return (nowValue - beforeValue)/beforeValue * 100;
        }

        /// <summary>
        /// 月同比,返回x%
        /// </summary>
        /// <param name="date"></param>
        /// <param name="powerStationId"></param>
        /// <param name="areaId"></param>
        /// <param name="deviceSN"></param>
        /// <param name="reflushKey">数据刷新机制,如果跟上次传的不一样就重新从数据中获取</param>
        /// <returns></returns>
        public async Task<decimal> TongBiOfMonthy(DateTime date, long powerStationId = 0, long areaId = 0, string deviceSN = "", string reflushKey = "")
        {
            var nowValue = await this.MonthValue(date, powerStationId, areaId, deviceSN, reflushKey);
            var beforeValue = await this.MonthValue(date.AddYears(-1), powerStationId, areaId, deviceSN, reflushKey);
            if (beforeValue == 0) return 0;
            return (nowValue - beforeValue) / beforeValue * 100;
        }
        /// <summary>
        /// 年同比,返回x%
        /// </summary>
        /// <param name="date"></param>
        /// <param name="powerStationId"></param>
        /// <param name="areaId"></param>
        /// <param name="deviceSN"></param>
        /// <param name="reflushKey">数据刷新机制,如果跟上次传的不一样就重新从数据中获取</param>
        /// <returns></returns>
        public async Task<decimal> TongBiOfYear(DateTime date, long powerStationId = 0, long areaId = 0, string deviceSN = "", string reflushKey = "")
        {
            var nowValue = await this.YearValue(date, powerStationId, areaId, deviceSN, reflushKey);
            var beforeValue = await this.YearValue(date.AddYears(-1), powerStationId, areaId, deviceSN, reflushKey);
            if (beforeValue == 0) return 0;
            return (nowValue - beforeValue) / beforeValue * 100;
        }


        private static MemoryCacheEntryOptions memoryCache {
            get {
                return null;//new MemoryCacheEntryOptions() { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10) };
            }
        }

        private static async Task<IEnumerable<vStatisticsData>> GetStatisticsData(DateTime start,DateTime end,decimal powerStationId,decimal areaId,string deviceSN, DbContextOptionsBuilder<MyDbContext> myDb, DbContextOptionsBuilder<DeviceDbContext> deviceDb)
        {
            using (var db = new DeviceDbContext(deviceDb.Options))
            {
                var q = db.vStatisticsData.Where(x => x.statisticsDataDate >= start && x.statisticsDataDate < end);
                if (powerStationId > 0)
                {
                    q = q.Where(x => x.statisticsStation_powerStationId_fk == powerStationId);
                }
                if (areaId > 0)
                {
                    q = q.Where(x => x.powerStationArea_keyId_fk == areaId);
                }
                if (!string.IsNullOrWhiteSpace(deviceSN))
                {
                    q = q.Where(x => x.deviceGroup == deviceSN);
                }
                return await q.ToListAsync();
            }
        }

        private static DataCache _PowerData = null;
        /// <summary>
        /// 发电电量 kw.h
        /// </summary>
        public static DataCache PowerData
        {
            get
            {

                //发电量
                if (_PowerData == null)
                {
                    _PowerData = new DataCache();
                    _PowerData.GetValueFromDb = async (start,end, powerStationId, areaId,deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.power);
                    };
                }
                return _PowerData;
            }
        }

        private static DataCache _TurbineFlow = null;
        /// <summary>
        /// 耗水量 立方米
        /// </summary>
        public static DataCache TurbineFlow
        {
            get
            {

                //发电量
                if (_TurbineFlow == null)
                {
                    _TurbineFlow = new DataCache();
                    _TurbineFlow.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.waterConsumption);
                    };
                }
                return _TurbineFlow;
            }
        }


        private static DataCache _PowerTimeData = null;
        /// <summary>
        /// 发电时长 小时
        /// </summary>
        public static DataCache PowerTimeData
        {
            get
            {

                
                if (_PowerTimeData == null)
                {
                    _PowerTimeData = new DataCache();
                    _PowerTimeData.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.powerGenerationTime)/((decimal)(3600));
                    };
                }
                return _PowerTimeData;
            }
        }


        private static DataCache _SalesData = null;
        /// <summary>
        /// 销售电量,并网电量,上网电量 kw.h
        /// </summary>
        public static DataCache SalesData
        {
            get
            {

                //销售电量
                if (_SalesData == null)
                {
                    _SalesData = new DataCache();
                    _SalesData.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.salePower);
                    };
                }
                return _SalesData;
            }
        }


        private static DataCache _SalesMoney = null;
        /// <summary>
        /// 月销售金额 元
        /// </summary>
        public static DataCache SalesMoney
        {
            get
            {

                //销售电量
                if (_SalesMoney == null)
                {
                    _SalesMoney = new DataCache();
                    _SalesMoney.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.saleMoney);
                    };
                }
                return _SalesMoney;
            }
        }


        private static DataCache _PlanDataOfMonth = null;
        /// <summary>
        /// 月计划计划发电量 kw.h
        /// </summary>
        public static DataCache PlanDataOfMonth
        {
            get
            {

                if (_PlanDataOfMonth == null)
                {
                    _PlanDataOfMonth = new DataCache();

                    _PlanDataOfMonth.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.planPowerOfMonth);
                    };
                }
                return _PlanDataOfMonth;
            }
        }

        private static DataCache _PlanDataOfYear = null;
        /// <summary>
        /// 月计划计划发电量 kw.h
        /// </summary>
        public static DataCache PlanDataOfYear
        {
            get
            {

                if (_PlanDataOfYear == null)
                {
                    _PlanDataOfYear = new DataCache();

                    _PlanDataOfYear.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.planPowerOfYear);
                    };
                }
                return _PlanDataOfYear;
            }
        }

        private static DataCache _planInstallDataMonth = null;
        /// <summary>
        /// 计划装机容量月 kw.h
        /// </summary>
        public static DataCache planInstallDataOfMonth
        {
            get
            {
                if (_planInstallDataMonth == null)
                {
                    _planInstallDataMonth = new DataCache();
                    _planInstallDataMonth.type = DataCacheEnum.累加型;
                    _planInstallDataMonth.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x=>x.planInstallOfMonth);
                    };
                }
                return _planInstallDataMonth;
            }
        }

        private static DataCache _planInstallDataYear = null;
        /// <summary>
        /// 计划装机容量年 kw.h
        /// </summary>
        public static DataCache planInstallDataYear
        {
            get
            {
                if (_planInstallDataYear == null)
                {
                    _planInstallDataYear = new DataCache();
                    _planInstallDataYear.type = DataCacheEnum.累加型;
                    _planInstallDataYear.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.planInstallOfYear);
                    };
                }
                return _planInstallDataYear;
            }
        }


        private static DataCache _InstallData = null;
        /// <summary>
        /// 装机容量 kw.h
        /// </summary>
        public static DataCache InstallData
        {
            get
            {
                if (_InstallData == null)
                {
                    _InstallData = new DataCache();
                    _InstallData.type = DataCacheEnum.单独计算;
                    _InstallData.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        using (var db = new DeviceDbContext(deviceDb.Options))
                        {
                            IQueryable<vStatisticsSingleData> q = db.vStatisticsSingleData;
                            if (powerStationId > 0)
                            {
                                q = q.Where(x => x.powerStationId == powerStationId);
                            }
                            if (areaId > 0)
                            {
                                q = q.Where(x => x.powerStationArea_keyId_fk == areaId);
                            }
                            if (!string.IsNullOrWhiteSpace(deviceSN))
                            {
                                q = q.Where(x => x.deviceGroup == deviceSN);
                            }
                            return await q.SumAsync(x => x.installed);
                        }
                    };
                }
                return _InstallData;
            }
        }

        private static DataCache _RainfallData = null;
        /// <summary>
        /// 降雨量 mm
        /// </summary>
        public static DataCache RainfallData
        {
            get
            {


                if (_RainfallData == null)
                {
                    _RainfallData = new DataCache();

                    _RainfallData.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.rainfall);
                    };
                }
                return _RainfallData;
            }
        }


        private static DataCache _PowerPeakData = null;
        /// <summary>
        /// 峰期发电电量 kw.h
        /// </summary>
        public static DataCache PowerPeakData
        {
            get
            {

                //发电量
                if (_PowerPeakData == null)
                {
                    _PowerPeakData = new DataCache();

                    _PowerPeakData.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.highPower);
                    };
                }
                return _PowerPeakData;
            }
        }

        private static DataCache _PowerFlatData = null;
        /// <summary>
        /// 平期发电电量 kw.h
        /// </summary>
        public static DataCache PowerFlatData
        {
            get
            {

                //发电量
                if (_PowerFlatData == null)
                {
                    _PowerFlatData = new DataCache();

                    _PowerFlatData.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.flatPower);
                    };
                }
                return _PowerFlatData;
            }
        }

        private static DataCache _PowerValleyData = null;
        /// <summary>
        /// 谷期发电电量 kw.h
        /// </summary>
        public static DataCache PowerValleyData
        {
            get
            {

                //发电量
                if (_PowerValleyData == null)
                {
                    _PowerValleyData = new DataCache();

                    _PowerValleyData.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.lowPower);
                    };
                }
                return _PowerValleyData;
            }
        }

        private static DataCache _LimitPowerData = null;
        /// <summary>
        /// 限购数据 kw.h
        /// </summary>
        public static DataCache LimitPowerData
        {
            get
            {
                //发电量
                if (_LimitPowerData == null)
                {
                    _LimitPowerData = new DataCache();

                    _LimitPowerData.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.limitPower);
                    };
                }
                return _LimitPowerData;
            }
        }

        private static DataCache _BuyPower = null;
        /// <summary>
        /// 购电量 kw.h
        /// </summary>
        public static DataCache BuyPower
        {
            get
            {
                //发电量
                if (_BuyPower == null)
                {
                    _BuyPower = new DataCache();

                    _BuyPower.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        return (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb)).Sum(x => x.buyPower);
                    };
                }
                return _BuyPower;
            }
        }


        private static DataCache _RunPower = null;
        /// <summary>
        /// 运行容量 kw.h
        /// </summary>
        public static DataCache RunPower
        {
            get
            {
                //发电量
                if (_RunPower == null)
                {
                    _RunPower = new DataCache();
                    _RunPower.type = DataCacheEnum.平均型;
                    _RunPower.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        var list = (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb));
                        return list.Any()?list.Average(x => x.runningCapacity):0;
                    };
                }
                return _RunPower;
            }
        }


        private static DataCache _activePower = null;
        /// <summary>
        /// 有功功率 kw.h
        /// </summary>
        public static DataCache ActivePower
        {
            get
            {
                //发电量
                if (_activePower == null)
                {
                    _activePower = new DataCache();
                    _activePower.type = DataCacheEnum.平均型;
                    _activePower.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        var v = (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb));
                        return v.Any()?v.Average(x => x.activePower):0;
                    };
                }
                return _activePower;
            }
        }

        private static DataCache _reactivePower = null;
        /// <summary>
        /// 无功功率 kw.h
        /// </summary>
        public static DataCache ReactivePower
        {
            get
            {
                //发电量
                if (_reactivePower == null)
                {
                    _reactivePower = new DataCache();
                    _reactivePower.type = DataCacheEnum.平均型;
                    _reactivePower.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        var v = (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb));
                        return v.Any() ? v.Average(x => x.reactivePower) : 0;
                    };
                }
                return _reactivePower;
            }
        }



        private static DataCache _factoryUsePower = null;
        /// <summary>
        /// 厂用电量 kw.h
        /// </summary>
        public static DataCache FactoryUsePower
        {
            get
            {
                //发电量
                if (_factoryUsePower == null)
                {
                    _factoryUsePower = new DataCache();
                    _factoryUsePower.GetValueFromDb = async (start, end, powerStationId, areaId, deviceSN, myDB, deviceDb) =>
                    {
                        var lst = (await GetStatisticsData(start, end, powerStationId, areaId, deviceSN, myDB, deviceDb));
                        foreach (var item in lst)
                        {
                            Console.WriteLine($"{item.statisticsDataDate},{item.statisticsDataId}厂用电为:{item.factoryUsePower}");
                        }
                        var v = lst.Sum(x => x.factoryUsePower);
                        Console.WriteLine($"{start}-{end}获取到的厂用电量为:{v}");
                        return v;
                    };
                }
                return _factoryUsePower;
            }
        }
    }
}
